﻿using MediatR;
using Traceability.App.Apilication.Common.Slaughter;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure.Interfance;

namespace Traceability.App.Apilication.Handler.Slaughter
{
    public class GetSlaughterHandler : IRequestHandler<GetSlaughterCommon, ApiPaimg<SlaughterModel>>
    {
        private readonly IBaseRespository<SlaughterModel> slaughterRepo;

        public GetSlaughterHandler(IBaseRespository<SlaughterModel> slaughterRepo)
        {
            this.slaughterRepo = slaughterRepo;
        }
        public Task<ApiPaimg<SlaughterModel>> Handle(GetSlaughterCommon request, CancellationToken cancellationToken)
        {
            //ApiResult<List<SlaughterModel>> res=new ApiResult<List<SlaughterModel>>();

            //var info = slaughterRepo.GetAll().Where(x => x.Batch == request.Batch).ToList();

            //res.Message = "根据生产批次查询屠宰信息";
            //res.Code = ApiEnum.Success;
            //res.Data = info;
            //return Task.FromResult(res);


            ApiPaimg<SlaughterModel> res = new ApiPaimg<SlaughterModel>();

            var info = slaughterRepo.GetAll().Where(x => x.Batch == request.Batch);

            res.ToTaCount=info.Count();
            res.ToTaPage = (int)Math.Ceiling(res.ToTaCount * 1.0 / request.pagesndex);
            res.PageData = info.OrderByDescending(x => x.Slaughter_ID).Skip((request.pageindex - 1) * request.pagesndex).Take(request.pagesndex).ToList();
            return Task.FromResult(res);

        }
    }
}
